package com.example.exam.sysResource.dao;

import com.example.exam.db.BaseDao;
import com.example.exam.sysResource.model.SysResourceBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class SysResourceDao extends BaseDao<SysResourceBean, Integer> {
    @Override
        /**
            * @Description:
            * @Param: []
            * @return: java.lang.String
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public String findByIdSql() {
        return "select * from sys_resource where id = ?";
    }
    /**
     * @Description:
     * @Param: [id]
     * @return: java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public List<Map<String, Object>> getResourceList(Integer id) {
        try {
            String sql = "select id, name, url, descr, permission, pid, seq, icon, type, status, create_time, create_user,is_quick,bg_color from sys_resource where status = 1 ";
            List<Object> list=new ArrayList<>();
            if (id!=null){
                sql+="and id=:id";
                list.add(id);
            }
            return getJtl().queryForList(sql,list.toArray());
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }
    @Override
        /**
            * @Description:
            * @Param: []
            * @return: java.lang.String
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public String getAddSql() {
        return "insert into sys_resource (name,url,descr,permission,pid,seq,icon,type,status,create_time,create_user,is_quick,bg_color) values (:name, :url, :descr, :permission, :pid, :seq, :icon, :type, :status, :createTime, :createUser,:isQuick,:bgColor) ";
    }
    /**
     * @Description:
     * @Param: [sysResourceBean]
     * @return: java.lang.Integer
     * @Author: xhw
     * @Date: 2021/7/26
     */ public Integer updateResource(SysResourceBean sysResourceBean){
        try {
            StringBuilder sql = new StringBuilder("UPDATE sys_resource SET ");
            List<String> updateSql = new ArrayList<String>();
            List<Object> params = new ArrayList<Object>();
            if (sysResourceBean.getName() != null && sysResourceBean.getName().isEmpty() == false) {
                updateSql.add("name = ? ");
                params.add(sysResourceBean.getName());
            }
            if (sysResourceBean.getUrl() != null && sysResourceBean.getUrl().isEmpty() == false) {
                updateSql.add("url = ? ");
                params.add(sysResourceBean.getUrl());
            }
            if (sysResourceBean.getDescr() != null && sysResourceBean.getDescr().isEmpty() == false) {
                updateSql.add("descr = ? ");
                params.add(sysResourceBean.getDescr());
            }
            if (sysResourceBean.getPermission() != null && sysResourceBean.getPermission().isEmpty() == false) {
                updateSql.add("permission = ? ");
                params.add(sysResourceBean.getPermission());
            }
            if (sysResourceBean.getPid() != null) {
                updateSql.add("pid = ? ");
                params.add(sysResourceBean.getPid());
            }
            if (sysResourceBean.getIcon() != null) {
                updateSql.add("icon = ? ");
                params.add(sysResourceBean.getIcon());
            }
            if (sysResourceBean.getSeq() != null) {
                updateSql.add("seq = ? ");
                params.add(sysResourceBean.getSeq());
            }
            if (sysResourceBean.getType() != null) {
                updateSql.add("type = ? ");
                params.add(sysResourceBean.getType());
            }
            if (sysResourceBean.getStatus() != null) {
                updateSql.add("status = ? ");
                params.add(sysResourceBean.getStatus());
            }
            if (sysResourceBean.getCreateTime() != null) {
                updateSql.add("create_time = ? ");
                params.add(sysResourceBean.getCreateTime());
            }
            if (sysResourceBean.getCreateUser() != null) {
                updateSql.add("create_user = ? ");
                params.add(sysResourceBean.getCreateUser());
            }
            if (sysResourceBean.getIsQuick() != null) {
                updateSql.add("is_quick = ? ");
                params.add(sysResourceBean.getIsQuick());
            }
            if(!StringUtils.isEmpty(sysResourceBean.getBgColor())) {
                updateSql.add("bg_color = ? ");
                params.add(sysResourceBean.getBgColor());
            }
            if (updateSql.isEmpty()) { // all fields is null, nothing to update
                return 0;
            }
            sql.append(String.join(", ", updateSql)).append(" WHERE id = ?");
            params.add(sysResourceBean.getId());
            return getJtl().update(sql.toString(), params.toArray());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return -1;
        }
    }/**
     * @Description:
     * @Param: [id]
     * @return: java.lang.Integer
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public Integer deleteResource(Integer id){
        String sql="UPDATE sys_resource SET STATUS=-1 WHERE id=?";
        Integer i=getJtl().update(sql,id);
        return i;
    }/**
     * @Description:
     * @Param: [pid]
     * @return: int
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public int queryMaxSeq(Integer pid) {
        try {
            String sql = "select max(seq) from sys_resource where pid = ? ";
            Integer maxSeq = getJtl().queryForObject(sql, Integer.class,  pid );
            if (maxSeq == null) {
                maxSeq = 1;
            }
            return maxSeq;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return 0;
        }
    }/**
     * @Description:
     * @Param: [pid]
     * @return: java.util.List<com.example.exam.sysResource.model.SysResourceBean>
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public List<SysResourceBean> queryResource(Integer pid) {
        String sql = "select * from sys_resource where status = 1 and pid =?";
        List<Object> list=new ArrayList<>();
        list.add(pid);
        return getJtl().query(sql,new BeanPropertyRowMapper<SysResourceBean>(SysResourceBean.class),list.toArray());
    }
    /**
     * @Description:
     * @Param: []
     * @return: java.util.List<com.example.exam.sysResource.model.SysResourceBean>
     * @Author: xhw
     * @Date: 2021/7/26
     */ public List<SysResourceBean> queryParentResource() {
        String sql = "select * from sys_resource where status = 1 and type = 1";
        return getJtl().query(sql,new BeanPropertyRowMapper<SysResourceBean>(SysResourceBean.class));
    }
}
